<?php


namespace app\tt\controller;


use app\common\model\Movie as MovieModel;
use app\common\model\WechatShareLink;
use think\Controller;
use think\facade\Request;

class Video extends Controller
{
    protected $middleware = [
        'app\http\middleware\checkTTUserToken::class'=>['except' 	=> ['read'] ],
    ];

    //指定用户的电影列表
    public function read()
    {
        $url_no= Request::param('no');
        $video= WechatShareLink::where('no',$url_no)->find();
        //如果腾讯视频
        //  if($video['url_mode']==2){

        //     $query = getUrlQuery( $video['video_url']);
        //     $vid = $query['vid'];

        //      $url = "http://vv.video.qq.com/getinfo?vids=".$vid."&platform=101001&charge=0&otype=json";
        //      $video['video_url'] = $this->txSolve($url);
        //      $video['vid'] = $vid;
        //  }else{
        //      $video['vid'] = '';
        //  }
        $this->result($video,200,'成功');
    }

    //解析腾讯视频
    public function txSolve($url)
    {
        $tencentVideoInfo = $this->curl_request($url);
        $tencentVideoJson = substr(explode('QZOutputJson=',$tencentVideoInfo)[1],0,-1);
        $tencentVideoArray = json_decode($tencentVideoJson,true);
        $fvkey = $tencentVideoArray['vl']['vi'][0]['fvkey'];
        $fn = $tencentVideoArray['vl']['vi'][0]['fn'];
        $url = $tencentVideoArray['vl']['vi'][0]['ul']['ui'][0]['url'];
        $video_url = $url.$fn.'?vkey='.$fvkey;
        return $video_url;
    }

    function curl_request($url,$https=true,$method='get',$data=null){
        //1.初识化curl
        $ch = curl_init($url);
        //2.根据实际请求需求进行参数封装
        //返回数据不直接输出
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        //如果是https请求
        if($https === true){
            curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
            curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
        }
        //如果是post请求
        if($method === 'post'){
            //开启发送post请求选项
            curl_setopt($ch,CURLOPT_POST,true);
            //发送post的数据
            curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
        }
        //3.发送请求
        $result = curl_exec($ch);
        //4.返回返回值，关闭连接
        curl_close($ch);
        return $result;
    }

}

